package com.sanery.dao;

import java.sql.ResultSet;

import com.dao.inter.UserDao;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.sanery.entity.User;

public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	@SuppressWarnings("null")
	@Override
	public User login(String username, String password) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = (Connection) this.getConn(); // 连接数据库
			@SuppressWarnings("unused")
			String sql = "select * from users where loginName=? and loginPwd=?";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt(1));
				user.setNickName(rs.getString(2));
				user.setLoginName(rs.getString(3));
				user.setLoginPwd(rs.getString(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(rs, ps, conn); // 关闭连接
		}
		return user;
	}

}
